
//
//  YYYHomeToolBar.swift
//  HotNews
//
//  Created by yang on 2017/10/24.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit

class YYYHomeToolBar: UIView {
    //转发
    private var retweetButton: UIButton?
    //评论
    private var commentButton: UIButton?
    //赞
    private var likeButton: UIButton?
    
    var viewModel: YYYHomeViewModel?
    {
        didSet
        {
            retweetButton?.setTitle(viewModel?.retweetString, for: .normal)
            commentButton?.setTitle(viewModel?.commentString, for: .normal)
            likeButton?.setTitle(viewModel?.likeString, for: .normal)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //初始化布局
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension YYYHomeToolBar
{
    func setupUI()
    {
        backgroundColor = UIColor.white
        let imageNameArray = ["timeline_icon_retweet", "timeline_icon_comment", "timeline_icon_unlike"]
        let titleNameArray = ["转发", "评论", "赞"]
        let btnWidth = width/CGFloat(3)
        let rect = CGRect(x: 0, y: 0, width: btnWidth, height: height)
        
        for i in 0..<titleNameArray.count
        {
            let bottomButton = UIButton(title: titleNameArray[i], fontSize: 11, normalColor: UIColor.darkGray, backgroundImageName: "timeline_card_bottom_background")
            bottomButton.tag = i
            bottomButton.frame = rect.offsetBy(dx: CGFloat(i) * btnWidth, dy: 0)
            bottomButton.setImage(UIImage(named: imageNameArray[i]), for: .normal)
            addSubview(bottomButton)
            bottomButton.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0)
            if i != 2
            {
                let middleImage = UIImageView(frame: CGRect(x: bottomButton.right, y: 0, width: 1, height: 20))
                middleImage.center.y = bottomButton.center.y
                middleImage.backgroundColor = UIColor.black
                addSubview(middleImage)
            }
            switch i {
            case 0:
                retweetButton = bottomButton
            case 1:
                commentButton = bottomButton
            case 2:
                 likeButton = bottomButton
            default:
                break
            }
        }
    }
}

